<template>
  <div class="modal-mask" v-show="modalShow">
    <div
      class="ui-modal"
      :style="{
        width: modalWidth + 'px',
        borderRadius: borderRadius + 'px',
        marginLeft: -modalWidth / 2 + 'px',
        top: position === 'center' ? '50%' : '50px',
        marginTop: position === 'center' ? -uiModalHeight / 2 + 'px' : '',
      }"
      ref="uiModal"
    >
      <header
        class="header"
        :style="{
          color: headerTextColor,
          backgroundColor: headerColor,
        }"
      >
        <!-- 标题名称 -->
        <h1>{{ headerText }}</h1>
        <!-- 右侧关闭图标 -->
        <a
          href="javascript:;"
          :style="{
            color: headerTextColor,
          }"
          @click="cancel"
          >&times;</a
        >
      </header>
      <article class="content">
        <p
          :style="{
            color: contentTextColor,
          }"
        >
          {{ contentText }}
        </p>
      </article>
      <!-- 底侧按钮群是否显示 -->
      <div class="btn-group" v-if="btnGroupShow">
        <button
          class="btn btn-confirm"
          :style="{
            backgroundColor: headerColor,
          }"
          @click="confirm"
        >
          {{ confirmBtnText }}
        </button>
        <button class="btn btn-cancel" @click="cancel">{{ cancelBtnText }}</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyModal",
  props: {
    modalShow: {
      type: Boolean,
      default: false,
    },
    modalWidth: {
      type: Number,
      default: 300,
    },
    borderRadius: {
      type: Number,
      default: 0,
    },
    headerColor: {
      type: String,
      default: "#000",
    },
    headerText: {
      type: String,
      default: "",
    },
    headerTextColor: {
      type: String,
      default: "#fff",
    },
    contentText: {
      type: String,
      default: "",
    },
    contentTextColor: {
      type: String,
      default: "#000",
    },
    position: {
      type: String,
      default: "top",
    },
    btnGroupShow: {
      type: Boolean,
      default: false,
    },
    confirmBtnText: {
      type: String,
      default: "确定",
    },
    cancelBtnText: {
      type: String,
      default: "取消",
    },
  },
  data() {
    return {
      uiModalHeight: 0,
      //子组件自用
      _modalShow: this.modalShow,
    };
  },
  methods: {
    confirm() {
      //点击完确认时就关闭模态框
      // this._modalShow = false;
      this.$emit('confirm');
    },
    cancel() {
      //点击完取消时就关闭模态框
      // this._modalShow = false;

      this.$emit('close');
    },
  },
  mounted() {
    // 组件挂载后获取ref引用里面marginTop高度给视图使用
    this.uiModalHeight = this.$refs.uiModal.offsetHeight;
  },
};
</script>

<style lang="scss" scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);

  a {
    text-decoration: none;
    outline: none;
    color: #333;
  }

  h1,
  p {
    font-weight: normal;
    margin: 0;
  }

  button {
    outline: none;
    border: 0;
  }

  .ui-modal {
    position: fixed;
    left: 50%;
    overflow: hidden;
    background-color: #fff;
    box-shadow: 1px 2px 3px #333;

    .header {
      height: 44px;
      padding: 0 15px;
      box-sizing: border-box;

      h1 {
        display: inline-block;
        font-size: 18px;
        line-height: 44px;
      }

      a {
        float: right;
        font-size: 20px;
        margin-top: 6px;
      }
    }

    .content {
      padding: 15px;
      box-sizing: border-box;
    }

    .btn-group {
      height: 30px;
      padding: 10px 15px;
      border-top: 1px solid #ddd;

      .btn {
        float: right;
        min-width: 80px;
        height: 30px;
        font-size: 14px;
        border-radius: 3px;
        cursor: pointer;

        &.btn-confirm {
          color: #fff;
          margin-left: 10px;
        }

        &.btn-cancel {
          background-color: #ddd;
          color: #666;
        }
      }
    }
  }
}
</style>
